package com.vd.wmt.ui;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.event.EventListenerList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vd.wmt.model.Status;
import com.vd.wmt.model.WageDetail;

public class WageTableModel implements TableModel{
	private Log log = LogFactory.getLog(WageTableModel.class);
	private static Map<Integer,String> posFieldMap= new HashMap<Integer,String>(){
		private static final long serialVersionUID = 1L;

	{
		put( 0,"email");
		put( 1,"YearMonth");
		put( 2,"name");
		put( 3,"basicWage");
		put( 4,"basicBonus");
		put( 5,"outSubsidy");
		put( 6,"lunchSubsidy");
		put( 7,"dinnerSubsidy");
		put( 8,"trafficSubsidy");
		put( 9,"houseSubsidy");
		put( 10,"travelSubsidy");
		put( 11,"managerSubsidy");
		put( 12,"workDays");
		put( 13,"OtherSubsidy");
		put( 14,"OtherDC");
		put( 15,"lateCharge");
		put( 16,"personIncomeTax");
		put( 17,"socialSecurityFund");
		put( 18,"housingFund");
		put( 19,"reagWage");
		put( 20,"wageInBoc");
		put( 21,"wageInOther");
		put( 22,"comment");
	}};
	private String[] columns=new String[] { 
			"状态"
			,"邮箱地址", "年月", "姓名", "基本工资", "基本奖金",
			"外派补贴", "中餐补贴", "晚餐补贴", "交通补贴", "住房补贴", "出差补贴", "管理津贴", "上班天数", "其他补贴", "其它补扣款", "迟到扣款", "个税", "社保",
			"公积金", "实发工资", "中行", "其它行", "备注" };
	private List<WageDetail> list = new ArrayList<WageDetail>();
	public List<WageDetail> getList() {
		return list;
	}
	/** List of listeners */
	protected EventListenerList listenerList = new EventListenerList();
	@Override
	public void addTableModelListener(TableModelListener l) {
		listenerList.add(TableModelListener.class,l);
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if(columnIndex == 0){
			return String.class;
		}
		Class<? extends WageDetail> clz = WageDetail.class;
		try {
			Field field= clz.getDeclaredField(posFieldMap.get(columnIndex-1));
			return field.getType();
		} catch (Exception e) {
			log.error("getColumnClass Exception",e);
		}
		return Object.class;
	}

	@Override
	public int getColumnCount() {
		return columns.length;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return columns[columnIndex];
	}

	@Override
	public int getRowCount() {
		return list.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		WageDetail wd = list.get(rowIndex);
		if(columnIndex == 0){
			return wd.getStatus();
		}
		String filedName = posFieldMap.get(columnIndex-1);
		String methodName= "get" + filedName.substring(0, 1).toUpperCase() + filedName.substring(1);
		try {
			Method mtd = wd.getClass().getMethod(methodName);
			return mtd.invoke(wd);
		} catch (Exception e) {
			log.error("getValueAt Exception", e);
		}
		return null;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		listenerList.remove(TableModelListener.class, l);
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		WageDetail wd;
		if(rowIndex<list.size()){
			 wd= list.get(rowIndex);
		}else{
			wd=new WageDetail();
			rowIndex=list.size()+1;
			list.add(wd);
		}
		if(columnIndex == 0){
			wd.setStatus((Status)value);
		}
		String filedName = posFieldMap.get(columnIndex-1);
		String methodName= "set" + filedName.substring(0, 1).toUpperCase() + filedName.substring(1);
		try {
			Field fld = wd.getClass().getDeclaredField(filedName);
			Method mtd = wd.getClass().getMethod(methodName,fld.getType());
			mtd.invoke(wd,value);
		} catch (Exception e) {
			log.error("getValueAt Exception", e);
		}
		TableModelEvent event =new TableModelEvent(this,rowIndex);
		fireEvent(event);
	}
	private void fireEvent(TableModelEvent event){
		Object[] listeners = listenerList.getListenerList();
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == TableModelListener.class) {
				((TableModelListener) listeners[i + 1])
						.tableChanged(new TableModelEvent(this));
			}
		}
		
	}
	public void AddWageDetail(List<WageDetail> list){
		this.list.addAll(list);
		TableModelEvent event = new TableModelEvent(this);
		fireEvent(event);
	}
	public void updateSendStatus(int row,Status status){
		WageDetail wd = list.get(row);
		wd.setStatus(status);
		fireEvent(new TableModelEvent(this,row));
	}

}
